#include <math.h>
#include <stdio.h>
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
#include <stdlib.h>     /* for atoi() */
#include <string.h>     /* for memset() */
#include <unistd.h>
#include "drawbot_math.h"

//#define WITH_NET

int global_x, global_y;
int global_d;
int global_a, global_b;

#ifdef WITH_NET
int sock;
struct sockaddr_in serv_addr;
unsigned short serv_port;
char *serv_ip;
char send_string[128];
unsigned int send_string_length;
#endif

void init(int d){
	global_x = 0;
	global_y = 0;
	global_d = d;

	global_a = 0;
	global_b = d;
//	printf("%d %d\n", global_a, global_b);
}

void draw_cartesian(int x, int y){
	float a, b;
	float local_a, local_b;
	float a_diff, b_diff;
	float factor;
	int i;
	
//	global_a = (int)sqrt( pow(global_x, 2) + pow(global_y, 2));
//	global_b = (int)sqrt( pow((global_d - global_x), 2) + pow(global_y, 2) );
	a = sqrt( pow(x, 2) + pow(y, 2) );
	b = sqrt( pow((global_d - x), 2) + pow(y, 2) );
	a_diff = a - global_a;
	b_diff = b - global_b;

//	printf("global_a: %d global_b: %d\n", global_a, global_b);
//	printf("a: %f b: %f\n", a, b);
//	printf("a_diff: %f b_diff: %f\n", a_diff, b_diff);

	local_a = global_a;
	local_b = global_b;

	if ( ((a_diff < 0) ? -a_diff : a_diff) > ((b_diff < 0) ? -b_diff : b_diff) ) {
		factor = a_diff / b_diff;
		for (i = 0; i < ((b_diff < 0) ? -b_diff : b_diff); i++) {
			local_a = (factor < 0) ? local_a - factor : local_a + factor;
			local_b = (factor < 0) ? local_b - 1 : local_b + 1;
//			printf("%d %d\n",(int)(a_diff * STEP_LENGTH),(int)(b_diff * STEP_LENGTH));
			printf("%d\t%d\n", (int)local_a, (int)local_b);
#ifdef WITH_NET
			sprintf(send_string, "%d\t%d\n", (int)local_a, (int)local_b);
			send_string_length = strlen(send_string);		/* Determine input length */

			/* Send the string to the server */
			if (send(sock, send_string, send_string_length, 0) != send_string_length) {
				// "send() sent a different number of bytes than expected"
				exit(1);
			}
#endif
		}
	}
	else {
		factor = b_diff / a_diff;
		for (i = 0; i < ((a_diff < 0) ? -a_diff : a_diff); i++) {
			local_b = (factor < 0) ? local_b - factor : local_b + factor;
			local_a = (factor < 0) ? local_a - 1 : local_a + 1;
//			printf("%d %d\n",(int)(a_diff * STEP_LENGTH),(int)(b_diff * STEP_LENGTH));
			printf("%d\t%d\n", (int)local_a, (int)local_b);
#ifdef WITH_NET
			sprintf(send_string, "%d\t%d\n", (int)local_a, (int)local_b);
			send_string_length = strlen(send_string);		/* Determine input length */

			/* Send the string to the server */
			if (send(sock, send_string, send_string_length, 0) != send_string_length) {
				// "send() sent a different number of bytes than expected"
				exit(1);
			}
#endif
		}
	}
	global_x = x;
	global_y = y;

	global_a = local_a;
	global_b = local_b;
}

int main(){
#ifdef WITH_NET
	serv_ip = "10.0.1.180";
	serv_port = 3333;
	if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
		// "socket() failed"
		exit(1);
	}

	memset(&serv_addr, 0, sizeof(serv_addr));		/* Zero out structure */
	serv_addr.sin_family = AF_INET;					/* Internet address family */
	serv_addr.sin_addr.s_addr = inet_addr(serv_ip);	/* Server IP address */
	serv_addr.sin_port = htons(serv_port);			/* Server port */

	/* Establish the connection to the echo server */
	if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
		// "connect() failed"
		exit(1);
	}
#endif
	init(150);
	draw_cartesian(51, -85);
	draw_cartesian(43, -112);
	draw_cartesian(51, -85);
//	interpolate_steps(43, 112);

#ifdef WITH_NET
	close(sock);
#endif
}
